package com.source.jucutils;

import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;

/**
 * @Author wengqy
 * @Date 2021/1/20
 */
public class SemaphoreDemo {
    public static void main(String[] args) {
        Semaphore  semaphore  =  new Semaphore(3);

        for (int i = 1; i <= 10; i++) {
            new  Thread(()->{

                try {
                    semaphore.acquire();
                    System.out.println("线程 "+Thread.currentThread().getName()+" 得到车位");
                    TimeUnit.MILLISECONDS.sleep(300);
                    System.out.println("线程 "+Thread.currentThread().getName()+" 离开车位 bye");

                } catch (InterruptedException e) {
                    e.printStackTrace();
                }finally {
                    semaphore.release();

                }


            },String.valueOf(i)).start();
        }
    }
}
